package cn.okcode.api.framework.result;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回结果处理工具类
 *
 * @author pyk13
 */
public class ResultUtil {

    public static Map<String, Object> ok(Object data) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", true);
        result.put("data", data);
        return result;
    }

    public static Map<String, Object> err(String msg) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", false);
        result.put("msg", msg);
        return result;
    }

    public static Map<String, Object> err(Exception e) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", false);
        result.put("msg", e.getLocalizedMessage());
        return result;
    }

    public static Map<String, Object> err(String msg, Throwable e) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", false);
        result.put("msg", msg + e.getLocalizedMessage());
        result.put("exception", e.toString());
        return result;
    }

    public static List<Map<String, String>> convertMapList(Map<String, String> map) {
        return convertMapList(map, "key", "value");
    }

    public static List<Map<String, String>> convertMapList(Map<String, String> map, String keyIdentifier, String valueIdentifier) {
        List<Map<String, String>> mapList = new ArrayList<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            Map<String, String> newMap = new HashMap<>(2);
            mapList.add(newMap);
            newMap.put(keyIdentifier, entry.getKey());
            newMap.put(valueIdentifier, entry.getValue());
        }
        return mapList;
    }
}
